{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "KataGo-Selfplay for Google Colab\n",
        "\n",
        "1.以下所有操作都是基于已经启动好环境的基础上进行的。\n",
        "\n",
        "2.此文件默认使用b18s999权重进行selfplay，且使用的是官方的selfplay1_maxsize9.cfg，用户可通过修改第一个单元格的权重下载链接来下载自己需要的权重（不要去掉双引号）。必须使用.bin.gz结尾的权重，因此6b10b15b20b等应在https://katagoarchive.org/g170/neuralnets/index.html 中下载\n",
        "\n",
        "3.可以使用官方的其他配置文件进行selfplay，它们在/root/KataGo/cpp/configs/training/中。可以双击需要使用的配置文件对文件内容进行修改。\n",
        "\n",
        "4.selfplay进行完毕后输出的文件在root目录下，名为data.zip，请不要忘记下载。"
      ],
      "metadata": {
        "id": "OtoeNxReAeiO"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ARWvotTdx7BY"
      },
      "outputs": [],
      "source": [
        "%cd ~\n",
        "!wget https://github.com/FengmingGo/katago_for_colaboratory/releases/download/20241228/libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb\n",
        "!dpkg -i libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb && rm libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb\n",
        "!wget https://libzip.org/download/libzip-1.11.2.tar.gz\n",
        "!tar -zxvf libzip-1.11.2.tar.gz && rm libzip-1.11.2.tar.gz\n",
        "%cd libzip-1.11.2\n",
        "!cmake .\n",
        "!make && make install\n",
        "!ldconfig /usr/local/lib && ldconfig /usr/local/lib64\n",
        "%cd ..\n",
        "!rm -rf libzip-1.11.2\n",
        "!git clone https://github.com/lightvector/KataGo.git\n",
        "!wget https://github.com/FengmingGo/katago_for_colaboratory/releases/download/20250202/selfplay\n",
        "!chmod 777 selfplay\n",
        "!mkdir data && mkdir models\n",
        "%cd models\n",
        "model_url = \"https://media.katagotraining.org/uploaded/networks/models/kata1/kata1-b18c384nbt-s9996604416-d4316597426.bin.gz\" #权重下载链接\n",
        "!wget {model_url}"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "max_games_total = 10000 #最多selfplay这么多盘\n",
        "config_file_path = \"./KataGo/cpp/configs/training/selfplay1_maxsize9.cfg\" #配置文件目录\n",
        "game_threads = 32 #同时这么多盘\n",
        "%cd ~\n",
        "!./selfplay selfplay -models-dir models -config {config_file_path} -output-dir data -max-games-total {max_games_total} -override-config numGameThreads={game_threads}\n",
        "!zip -r data.zip data"
      ],
      "metadata": {
        "id": "AROa1Bdj4x7a"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}